package com.twotiger.and.util;

import android.util.Log;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 日志记录
 * 
 */
public class LogUtil {
    /**
     * 开发阶段
     */
    private static final int DEVELOP = 0;
    /**
     * 内部测试阶段
     */
    private static final int DEBUG = 1;
    /**
     * 公开测试
     */
    private static final int BATE = 2;
    /**
     * 正式版
     */
    private static final int RELEASE = 3;

    /**
     * 当前阶段标示
     */
//    private static int currentStage = DEVELOP;  //开发用
    private static int currentStage = RELEASE;  //线上发布用

    private static FileOutputStream outputStream;
    private static String pattern = "yyyy-MM-dd HH:mm:ss";

    public static void info(String msg) {
        info(LogUtil.class, msg);
    }

    public static void info(@SuppressWarnings("rawtypes") Class clazz, String msg) {

        switch (currentStage) {
        case DEVELOP:
            // output to the console
            Log.i(clazz.getSimpleName(), msg);
            break;
        case DEBUG:
            // 在应用下面创建目录存放日志,启动上传

            break;
        case BATE:
            // write to sdcard
            String time = DateUtil.getFormatDate(pattern);
            if (Tools.hasSdcard()) {
                if (outputStream != null && Tools.getSDFreeSize() > 1) {
                    try {
                        outputStream.write(time.getBytes());
                        String className = "";
                        if (clazz != null) {
                            className = clazz.getSimpleName();
                        }
                        outputStream.write(("    " + className + "\r\n").getBytes());

                        outputStream.write(msg.getBytes());
                        outputStream.write("\r\n".getBytes());
                        outputStream.flush();
                    } catch (IOException e) {

                    }
                } else {
                    Log.i("SDCAEDTAG", "file is null or storage insufficient");
                }
            }
            break;
        case RELEASE:
            // 一般不做日志记录
            break;
        }
    }
}
